/*
 * Copyright (c) 2022 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import StringBuilder from '../../util/StringBuilder.js';
import IllegalArgumentException from '../../IllegalArgumentException.js';
import StringUtils from '../../common/StringUtils.js';
import StandardCharsets from '../../util/StandardCharsets.js';
import SymbolInfo from './SymbolInfo.js';
import SymbolShapeHint from './SymbolShapeHint.js';
/*final*/
export default class EncoderContext {
    constructor(msg) {
        this.pos = 0;
        this.newEncoding = 0;
        this.skipAtEnd = 0;
        // From this point on Strings are not Unicode anymore!
        let msgBinary = StringUtils.getBytes(msg, StandardCharsets.ISO_8859_1);
        let sb = new StringBuilder();
        for (let i = 0, c = msgBinary.length; i < c; i++) {
            let ch = /*(char)*/ (msgBinary[i] & 0xff);
            if (ch === StringUtils.getCharCode('?') && StringUtils.getCharCode(msg.charAt(i)) !== StringUtils.getCharCode('?')) {
                throw new IllegalArgumentException('Message contains characters outside ISO-8859-1 encoding.');
            }
            sb.append(ch);
        }
        this.msg = sb.toString(); // Not Unicode here!
        this.shape = SymbolShapeHint.FORCE_NONE;
        this.codewords = new StringBuilder();
        this.newEncoding = -1;
    }
    setSymbolShape(shape) {
        this.shape = shape;
    }
    setSizeConstraints(minSize, maxSize) {
        this.minSize = minSize;
        this.maxSize = maxSize;
    }
    getMessage() {
        return this.msg;
    }
    setSkipAtEnd(count) {
        this.skipAtEnd = count;
    }
    getCurrentChar() {
        return StringUtils.getCharCode(this.msg.charAt(this.pos));
    }
    getCurrent() {
        return StringUtils.getCharCode(this.msg.charAt(this.pos));
    }
    getCodewords() {
        return this.codewords;
    }
    writeCodewords(codewords) {
        this.codewords.append(codewords);
    }
    writeCodeword(codeword) {
        this.codewords.append(codeword);
    }
    getCodewordCount() {
        return this.codewords.length();
    }
    getNewEncoding() {
        return this.newEncoding;
    }
    signalEncoderChange(encoding) {
        this.newEncoding = encoding;
    }
    resetEncoderSignal() {
        this.newEncoding = -1;
    }
    hasMoreCharacters() {
        return this.pos < this.getTotalMessageCharCount();
    }
    getTotalMessageCharCount() {
        return this.msg.length - this.skipAtEnd;
    }
    getRemainingCharacters() {
        return this.getTotalMessageCharCount() - this.pos;
    }
    getSymbolInfo() {
        return this.symbolInfo;
    }
    updateSymbolInfoNoArgs() {
        this.updateSymbolInfo(this.getCodewordCount());
    }
    updateSymbolInfo(len) {
        if (this.symbolInfo == null || len > this.symbolInfo.getDataCapacity()) {
            this.symbolInfo = SymbolInfo.lookup(len, this.shape, this.minSize, this.maxSize, true);
        }
    }
    resetSymbolInfo() {
        this.symbolInfo = null;
    }
}
